package com.android.boncen.commonFrame.fragment;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.boncen.commonFrame.R;
import com.android.boncen.commonFrame.adapter.DocAdapter;
import com.android.boncen.commonFrame.base.BaseFragment;
import com.android.boncen.commonFrame.bean.CollectionBean;
import com.android.boncen.commonFrame.bean.DoctorBean;
import com.android.boncen.commonFrame.bean.UserBean;
import com.android.boncen.commonFrame.tools.DesityUtil;

import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.SaveListener;

/***
 * 医生
 */
public class DoctorFragment extends BaseFragment {
    private TextView title;
    private ListView lvDoc;
    DocAdapter adapter;
    int page = 1;
    private ProgressDialog progressDialog;


    //    @Override
    protected View initView() {
        View rootView = View.inflate(getContext(), R.layout.list_fragment, null);
        title = (TextView) rootView.findViewById(R.id.tv_title);
        lvDoc = (ListView) rootView.findViewById(R.id.lvDoc);
        lvDoc.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                // 当不滚动时
                if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) {
                    // 判断是否滚动到底部
                    if (view.getLastVisiblePosition() == view.getCount() - 1) {
                        //加载更多功能的代码
                        page++;
                        query(page);
                    }
                }
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

            }
        });
        return rootView;
    }

    @Override
    protected void initData() {
        super.initData();
        title.setText("医生");
        adapter = new DocAdapter(getContext(), new DocAdapter.Callback() {
            @Override
            public void onClick(DoctorBean doctorBean) {
                addCollection(getActivity(), doctorBean);
            }
        });
        lvDoc.setAdapter(adapter);
        query(page);
        progressDialog = new ProgressDialog(getActivity());

    }

    private void query(int page) {
        BmobQuery<DoctorBean> query = new BmobQuery<DoctorBean>();
        query.setSkip(20 * (page - 1));
        query.setLimit(20);
        query.findObjects(new FindListener<DoctorBean>() {
            @Override
            public void done(List<DoctorBean> object, BmobException e) {
                if (e == null) {
                    adapter.setData(object);
                } else {

                }
            }
        });
    }


    private void addCollection(final Context context, final DoctorBean doctorBean) {
        progressDialog.show();
        UserBean user = BmobUser.getCurrentUser(UserBean.class);

        BmobQuery<CollectionBean> query = new BmobQuery<CollectionBean>();
        query.addWhereEqualTo("deviceId", user.getObjectId());    // 查询当前用户的所有帖子
        query.addWhereEqualTo("doc", doctorBean);    // 查询当前用户的所有帖子
        query.order("-updatedAt");
        query.include("doc");// 希望在查询帖子信息的同时也把发布人的信息查询出来
        query.findObjects(new FindListener<CollectionBean>() {

            @Override
            public void done(List<CollectionBean> object, BmobException e) {


                if (object != null && !object.isEmpty()) {
                    progressDialog.dismiss();
                    Toast.makeText(getActivity(), "已收藏该医生", Toast.LENGTH_SHORT).show();
                } else {

                    save(doctorBean);
                }
            }

        });


    }

    private void save(DoctorBean doctorBean) {
        UserBean user = BmobUser.getCurrentUser(UserBean.class);
        CollectionBean bean = new CollectionBean();
        bean.setDeviceId(user.getObjectId());
        bean.setDoc(doctorBean);
        bean.save(new SaveListener<String>() {
            @Override
            public void done(String s, BmobException e) {
                progressDialog.dismiss();
                if (e == null) {
                    Toast.makeText(getActivity(), "收藏成功", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(getActivity(), "收藏失败", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}
